home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / bbs / fcfgw40s.zip / TIPDLG.CPP < prev    next >
C/C++ Source or Header  |  1996-04-18  |  6KB  |  213 lines

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "tipdlg.h"
  4. // CG: This file added by 'Tip of the Day' component.
  5.  
  6. #include <winreg.h>
  7. #include <sys\stat.h>
  8. #include <sys\types.h>
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CTipDlg dialog
  18.  
  19. #define MAX_BUFLEN 1000
  20.  
  21. static const TCHAR szSection[] = _T("Tip");
  22. static const TCHAR szIntFilePos[] = _T("FilePos");
  23. static const TCHAR szTimeStamp[] = _T("TimeStamp");
  24. static const TCHAR szIntStartup[] = _T("StartUp");
  25.  
  26. CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/)
  27.     : CDialog(IDD_TIP, pParent)
  28. {
  29.     //{{AFX_DATA_INIT(CTipDlg)
  30.     m_bStartup = TRUE;
  31.     //}}AFX_DATA_INIT
  32.  
  33.     // We need to find out what the startup and file position parameters are
  34.     // If startup does not exist, we assume that the Tips on startup is checked TRUE.
  35.     CWinApp* pApp = AfxGetApp();
  36.     m_bStartup = !pApp->GetProfileInt(szSection, szIntStartup, 0);
  37.     UINT iFilePos = pApp->GetProfileInt(szSection, szIntFilePos, 0);
  38.  
  39.     // Now try to open the tips file
  40.     m_pStream = fopen("filecfg.tip", "r");
  41.     if (m_pStream == NULL) 
  42.     {
  43.         m_strTip.LoadString(CG_IDS_FILE_ABSENT);
  44.         return;
  45.     } 
  46.  
  47.     // If the timestamp in the INI file is different from the timestamp of
  48.     // the tips file, then we know that the tips file has been modified
  49.     // Reset the file position to 0 and write the latest timestamp to the
  50.     // ini file
  51.     struct _stat buf;
  52.     _fstat(_fileno(m_pStream), &buf);
  53.     CString strCurrentTime = ctime(&buf.st_ctime);
  54.     strCurrentTime.TrimRight();
  55.     CString strStoredTime = 
  56.         pApp->GetProfileString(szSection, szTimeStamp, NULL);
  57.     if (strCurrentTime != strStoredTime) 
  58.     {
  59.         iFilePos = 0;
  60.         pApp->WriteProfileString(szSection, szTimeStamp, strCurrentTime);
  61.     }
  62.  
  63.     if (fseek(m_pStream, iFilePos, SEEK_SET) != 0) 
  64.     {
  65.         AfxMessageBox(CG_IDP_FILE_CORRUPT);
  66.     }
  67.     else 
  68.     {
  69.         GetNextTipString(m_strTip);
  70.     }
  71. }
  72.  
  73. CTipDlg::~CTipDlg()
  74. {
  75.     // This destructor is executed whether the user had pressed the escape key
  76.     // or clicked on the close button. If the user had pressed the escape key,
  77.     // it is still required to update the filepos in the ini file with the 
  78.     // latest position so that we don't repeat the tips! 
  79.     
  80.     // But make sure the tips file existed in the first place....
  81.     if (m_pStream != NULL) 
  82.     {
  83.         CWinApp* pApp = AfxGetApp();
  84.         pApp->WriteProfileInt(szSection, szIntFilePos, ftell(m_pStream));
  85.         fclose(m_pStream);
  86.     }
  87. }
  88.         
  89. void CTipDlg::DoDataExchange(CDataExchange* pDX)
  90. {
  91.     CDialog::DoDataExchange(pDX);
  92.     //{{AFX_DATA_MAP(CTipDlg)
  93.     DDX_Check(pDX, IDC_STARTUP, m_bStartup);
  94.     DDX_Text(pDX, IDC_TIPSTRING, m_strTip);
  95.     //}}AFX_DATA_MAP
  96. }
  97.  
  98. BEGIN_MESSAGE_MAP(CTipDlg, CDialog)
  99.     //{{AFX_MSG_MAP(CTipDlg)
  100.     ON_BN_CLICKED(IDC_NEXTTIP, OnNextTip)
  101.     ON_WM_CTLCOLOR()
  102.     ON_WM_PAINT()
  103.     //}}AFX_MSG_MAP
  104. END_MESSAGE_MAP()
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CTipDlg message handlers
  108.  
  109. void CTipDlg::OnNextTip()
  110. {
  111.     GetNextTipString(m_strTip);
  112.     UpdateData(FALSE);
  113. }
  114.  
  115. void CTipDlg::GetNextTipString(CString& strNext)
  116. {
  117.     LPTSTR lpsz = strNext.GetBuffer(MAX_BUFLEN);
  118.  
  119.     // This routine identifies the next string that needs to be
  120.     // read from the tips file
  121.     BOOL bStop = FALSE;
  122.     while (!bStop) 
  123.     {
  124.         if (_fgetts(lpsz, MAX_BUFLEN, m_pStream) == NULL) 
  125.         {
  126.             // We have either reached EOF or enocuntered some problem
  127.             // In both cases reset the pointer to the beginning of the file
  128.             // This behavior is same as VC++ Tips file
  129.             if (fseek(m_pStream, 0, SEEK_SET) != 0) 
  130.                 AfxMessageBox(CG_IDP_FILE_CORRUPT);
  131.         } 
  132.         else 
  133.         {
  134.             if (*lpsz != ' ' && *lpsz != '\t' && 
  135.                 *lpsz != '\n' && *lpsz != ';') 
  136.             {
  137.                 // There should be no space at the beginning of the tip
  138.                 // This behavior is same as VC++ Tips file
  139.                 // Comment lines are ignored and they start with a semicolon
  140.                 bStop = TRUE;
  141.             }
  142.         }
  143.     }
  144.     strNext.ReleaseBuffer();
  145. }
  146.  
  147. HBRUSH CTipDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  148. {
  149.     if (pWnd->GetDlgCtrlID() == IDC_TIPSTRING)
  150.         return (HBRUSH)GetStockObject(WHITE_BRUSH);
  151.  
  152.     return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  153. }
  154.  
  155. void CTipDlg::OnOK()
  156. {
  157.     CDialog::OnOK();
  158.     
  159.     // Update the startup information stored in the INI file
  160.     CWinApp* pApp = AfxGetApp();
  161.     pApp->WriteProfileInt(szSection, szIntStartup, !m_bStartup);
  162. }
  163.  
  164. BOOL CTipDlg::OnInitDialog()
  165. {
  166.     CDialog::OnInitDialog();
  167.  
  168.     // If Tips file does not exist then disable NextTip
  169.     if (m_pStream == NULL)
  170.         GetDlgItem(IDC_NEXTTIP)->EnableWindow(FALSE);
  171.  
  172.     return TRUE;  // return TRUE unless you set the focus to a control
  173. }
  174.  
  175. void CTipDlg::OnPaint()
  176. {
  177.     CPaintDC dc(this); // device context for painting
  178.  
  179.     // Get paint area for the big static control
  180.     CWnd* pStatic = GetDlgItem(IDC_BULB);
  181.     CRect rect;
  182.     pStatic->GetWindowRect(&rect);
  183.     ScreenToClient(&rect);
  184.  
  185.     // Paint the background white.
  186.     CBrush brush;
  187.     brush.CreateStockObject(WHITE_BRUSH);
  188.     dc.FillRect(rect, &brush);
  189.  
  190.     // Load bitmap and get dimensions of the bitmap
  191.     CBitmap bmp;
  192.     bmp.LoadBitmap(IDB_LIGHTBULB);
  193.     BITMAP bmpInfo;
  194.     bmp.GetBitmap(&bmpInfo);
  195.  
  196.     // Draw bitmap in top corner and validate only top portion of window
  197.     CDC dcTmp;
  198.     dcTmp.CreateCompatibleDC(&dc);
  199.     dcTmp.SelectObject(&bmp);
  200.     rect.bottom = bmpInfo.bmHeight + rect.top;
  201.     dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), 
  202.         &dcTmp, 0, 0, SRCCOPY);
  203.  
  204.     // Draw out "Did you know..." message next to the bitmap
  205.     CString strMessage;
  206.     strMessage.LoadString(CG_IDS_DIDYOUKNOW);
  207.     rect.left += bmpInfo.bmWidth;
  208.     dc.DrawText(strMessage, rect, DT_VCENTER | DT_SINGLELINE);
  209.  
  210.     // Do not call CDialog::OnPaint() for painting messages
  211. }
  212.  
  213.